package com.ang.springboot.provider;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.ang.springboot.entity.UserInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/provider")
@RestController
public class ProviderController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 直连交换机发送消息
     */
    @RequestMapping("/sendDirect")
    public String sendDirect(){
        Map map = new HashMap<>();
        map.put("msg","sendDirect");
        map.put("time", DateUtil.now());

        rabbitTemplate.convertAndSend("directExchange","directKey",JSON.toJSONString(map));
        return "success";
    }

    /**
     * 直连交换机发送消息：对象JSON
     */
    @RequestMapping("/sendDirect1")
    public String sendDirect1(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId("1");
        userInfo.setName("test");

        rabbitTemplate.convertAndSend("directExchange","directKey", JSON.toJSONString(userInfo));
        return "success";
    }

    /**
     * 扇形交换机发送消息
     * fanoutExchange绑定了2个队列，2个队列都会收到同一条消息
     */
    @RequestMapping("/sendFanout")
    public String sendFanout(){
        Map map = new HashMap<>();
        map.put("msg","sendFanout");
        map.put("time", DateUtil.now());

        //fanout不需要routKey，即使加上routKey也没有效果，但是第二个参数要写null，否则"fanoutExchange"这个交换机收不到消息
        rabbitTemplate.convertAndSend("fanoutExchange",null,map);
        return "success";
    }

    /**
     * topic交换机发送消息
     */
    @RequestMapping("/sendTopicMan")
    public String sendTopicMan(){
        Map map = new HashMap<>();
        map.put("msg","man");
        map.put("time", DateUtil.now());

        rabbitTemplate.convertAndSend("topicExchange","topic.man",map);
        return "success";
    }

    /**
     * topic交换机发送消息
     */
    @RequestMapping("/sendTopicWoman")
    public String sendTopicWoman(){
        Map map = new HashMap<>();
        map.put("msg","woman");
        map.put("time", DateUtil.now());

        rabbitTemplate.convertAndSend("topicExchange","topic.woman",map);
        return "success";
    }

    /**
     * 发送一个不存在的Exchange
     */
    @RequestMapping("/sendNoExchange")
    public String sendNoExchange(){
        Map map = new HashMap<>();
        map.put("msg","noExchange");
        map.put("time", DateUtil.now());

        rabbitTemplate.convertAndSend("noExchange",null,map);
        return "success";
    }

    /**
     * 发送一个错误路由键的消息
     * 消息不能正确发送到queue
     */
    @RequestMapping("/sendNoRoutKey")
    public String sendNoRoutKey(){
        Map map = new HashMap<>();
        map.put("msg","noRoutKey");
        map.put("time", DateUtil.now());

        rabbitTemplate.convertAndSend("topicExchange","noRoutKey",map);
        return "success";
    }


}
